package org.nanotek;

import java.sql.Connection;

import javax.annotation.ManagedBean;
import javax.annotation.PostConstruct;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
import javax.sql.DataSource;

@ManagedBean
public class TestMainClass {

	private Boolean datasourceActive = false;
    private String	testDs = "testDs";

    private EntityManagerFactory entitManagerFactory;
    @PersistenceContext(name="musicbrainz")
    private EntityManager entityManager; 
    
	@PostConstruct
	public void testDs()
	
	{
//		// Obtain our environment naming context
		Context initCtx;
		try {
			initCtx = new InitialContext();
		
			Context envCtx = (Context) initCtx.lookup("");

		// Look up our data source
			DataSource ds = (DataSource)envCtx.lookup("jdbc/__Postgres");

		// Allocate and use a connection from the pool
		Connection conn = ds.getConnection();
//		... use this connection to access the database ...
		conn.close();
//		entitManagerFactory = Persistence.createEntityManagerFactory("musicbrainz");
//		entityManager = entitManagerFactory.createEntityManager()   ;
				
	    datasourceActive = true;// entityManager.isOpen();
		
		} catch (Throwable e) {
			e.printStackTrace();
//			throw new RuntimeException(e);
		}
	}

	public Boolean getDatasourceActive() {
		return datasourceActive;
	}

	public void setDatasourceActive(Boolean datasourceActive) {
		this.datasourceActive = datasourceActive;
	}

	public String getTestDs() {
		return testDs;
	}

	public void setTestDs(String testDs) {
		this.testDs = testDs;
	}
}
